package com.cskaoyan.controller;

import com.cskaoyan.aspectj.order.OrderDelete;
import com.cskaoyan.aspectj.order.OrderRefund;
import com.cskaoyan.aspectj.order.OrderShip;
import com.cskaoyan.bean.MarketLogisticsChannel;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.AdminOrderListVo;
import com.cskaoyan.bean.vo.OrderDetailVo;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.lang.model.element.VariableElement;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Since 2022/12/29 13:57
 * @Author LA
 * @Description: 处理订单管理相关功能
 */
@RestController
@RequestMapping("admin/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    // http://182.92.235.201:8083/admin/order/list?page=1&limit=20&
    // orderStatusArray=301&sort=add_time
    // &order=desc&orderId=&start=&end=&userId=1&orderSn=20221125693986
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info, Short[] orderStatusArray, String orderId,
                           Date start, Date end, Date[] timeArray,
                           String userId, String orderSn) {

        CommonData<AdminOrderListVo> data = orderService.list(info, orderStatusArray, orderId, start, end, timeArray, userId, orderSn);
        return BaseRespVo.ok(data);
    }

    @OrderDelete
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map map) {
        // {orderId: 18}
        // 利用map接收 再取值
        Integer orderId = (Integer) map.get("orderId");
        orderService.delete(orderId);
        // 响应
        // {"errno":0,"errmsg":"成功"}
        return new BaseRespVo().ok("成功");
    }

    @RequestMapping("reply")
    // http://182.92.235.201:8083/admin/order/reply
    public BaseRespVo reply(@RequestBody Map map) {
        // 负载   {commentId: 14, content: "这个没回复吧？"}
        Integer id = (Integer) map.get("commentId");
        String adminContent = (String) map.get("content");

        // 通过id查评论
        String queryContent = orderService.selectCommentById(id);
        BaseRespVo respVo = BaseRespVo.ok();
        // 两种响应情况
        // 查到评论的有数据 说明已经回复了
        if (!StringUtil.isEmpty(queryContent)) {
            // {"errno":622,"errmsg":"订单商品已回复！"}
            respVo.setErrno(622);
            respVo.setErrmsg("订单商品已回复！");
            return respVo;
        }
        // 没查到 则更新评论表
        orderService.updateCommentById(id, adminContent);
        // {"errno":0,"errmsg":"成功"}
        respVo.setErrno(0);
        respVo.setErrmsg("成功");
        return respVo;
    }

    // 订单详情
    @RequestMapping("detail")
    // 请求 URL: http://182.92.235.201:8083/admin/order/detail?id=314 请求方法: GET
    public BaseRespVo detail(String id) {
        // id的长度校验
        if (id != null && id.length() > 10) {
            BaseRespVo baseRespVo = BaseRespVo.invalidParameter("参数值不对");
            // 参数违规
            baseRespVo.setErrno(402);
            return baseRespVo;
        }
        OrderDetailVo data = orderService.detail(id);
        return BaseRespVo.ok(data, "成功");

    }

    // 发货
    @OrderShip
    @RequestMapping(value = "ship",method = RequestMethod.POST)
    // 请求 URL: http://182.92.235.201:8083/admin/order/ship 请求方法: post
    public BaseRespVo ship(@RequestBody Map map) {
        // {
        //   "orderId": 310,
        //   "shipChannel": "ZTO",
        //   "shipSn": "la123456789"
        // }
        Integer orderId = (Integer) map.get("orderId");
        String shipChannel = (String) map.get("shipChannel");
        String shipSn = (String) map.get("shipSn");
        orderService.ship(orderId, shipChannel, shipSn);
        return BaseRespVo.ok("成功");

    }

    // 退款
    @OrderRefund
    @RequestMapping("refund")
    // 请求 URL: http://182.92.235.201:8083/admin/order/refund
    public BaseRespVo refund(@RequestBody Map map) {
        //{orderId: 203, refundMoney: 156}
        Integer orderId = (Integer) map.get("orderId");
        // refundAmount 退款金额 等于 actualPrice 实付金额
        Integer refundMoneyReceive = (Integer) map.get("refundMoney");
        // 转成 BigDecimal类型
        BigDecimal refundMoney = new BigDecimal(Integer.parseInt(refundMoneyReceive.toString()));
        orderService.refund(orderId, refundMoney);
        return BaseRespVo.ok("成功");
    }

    @RequestMapping("channel")
    //请求 URL: http://182.92.235.201:8083/admin/order/channel
    // 请求方法: GET
    public BaseRespVo channel() {
        List<MarketLogisticsChannel> date = orderService.channel();
        return BaseRespVo.ok(date, "成功");
    }
}